//
//  BranchCourseVC.m
//  ZhiBoYuan
//
//  Created by JW on 2018/4/27.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "BranchCourseVC.h"

#import "AdetailViewController.h"
#import "MicroCourseModel.h"

#import "ABCell.h"
static NSString * identB = @"ABCell";

@interface BranchCourseVC ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign)int page;

@end

@implementation BranchCourseVC


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = self.titleName;
    
    _page = 1;
    
    [self initWithViews];
    
    [self downloadBranchCourseDatas];
}

- (void)downloadBranchCourseDatas{
    
    SGParameter *parameter  = [[SGParameter alloc] init];
    parameter.body = @{
                       @"page":[NSString stringWithFormat:@"%d",_page],
                       @"page_count":@"10",
                       @"fenyuan_id":self.fenyuan_id// 1 直播 2微课 3套餐
                       };
    [SGHttpTool POST:Host_Append(BranchCourseList) parameter:parameter sucess:^(id responseObject) {
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        if (_page == 1) {
            [_dataArray removeAllObjects];
        }
        
        ZYLog(@"%@",responseObject);
        SGReceive *receive = [SGReceive receiveWithResponseObject:responseObject dataWord:@"course_list" dataClass:[MicroCourseModel class]];
//        [self.dataArray addObjectsFromArray:receive.data];
        
        for (NSDictionary *dicc in receive.data) {
            [self.dataArray addObject:dicc];
        }
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
    
}


#pragma mark -- UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ABCell *cell = [tableView dequeueReusableCellWithIdentifier:identB forIndexPath:indexPath];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    cell.microModel = self.dataArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    AdetailViewController *vc= [[AdetailViewController alloc]initWithNibName:@"AdetailViewController" bundle:nil];
//    vc.course_id = [self.dataArray[indexPath.row] microCourseID];
//    vc.hidesBottomBarWhenPushed =YES;
//
//    [self.navigationController pushViewController:vc animated:YES];
    
    
        [Helper keChengDetailFromVC:self course_id:[self.dataArray[indexPath.row] microCourseID]];
    
}

#pragma mark - 视图
- (void)initWithViews{
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    
}

#pragma makr -- property
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}




- (UITableView *)tableView {
    if (!_tableView) {
        UITableView *  tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, ScreenWidth, Screenheight- NAVIGATION_BAR_HEIGHT ) style:UITableViewStylePlain];
        tableView.backgroundColor = [UIColor clearColor];
        tableView.delegate        = self;
        tableView.dataSource      = self;
        //        tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        
        tableView.estimatedRowHeight = 44.0f;//推测高度，必须有，可以随便写多少
        
        tableView.rowHeight = UITableViewAutomaticDimension;//iOS8之后默认就是这个值，可以省略
        
        //        self.extendedLayoutIncludesOpaqueBars = YES;
        
        if (@available(iOS 11.0, *)) {
            tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        tableView.contentInset = UIEdgeInsetsMake(0, 0, 49, 0);
        tableView.scrollIndicatorInsets = _tableView.contentInset;
        
        [tableView registerNib:[UINib nibWithNibName:identB bundle:nil] forCellReuseIdentifier:identB];
        
        [tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
        __weak __typeof(&*self)weakSelf = self;
        
//         下拉刷新
                tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
                    _page = 1;
        
                    [self downloadBranchCourseDatas];
                    
                        [weakSelf.tableView.mj_header endRefreshing];
                }];
        
                // 上拉加载
                tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                    
                    _page = _page + 1;
        
                    [self downloadBranchCourseDatas];
                    
                }];
        _tableView = tableView;
        
    }
    return _tableView;
}

@end
